//
//  HomeViewController.swift
//  liveDemo
//
//  Created by satoshi_uma on 2020/10/20.
//  Copyright © 2020 satoshi_uma. All rights reserved.
//

import UIKit

private let titleViewH: CGFloat = 40

class HomeViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }

    //    懒加载 PageTitleView
    private lazy var pageTitleView: PageTitleView = {
        
        let titleFrame = CGRect(x: 0, y: mcNavBarAndStatusBarHeight, width: SCREEN_WIDTH, height: titleViewH)
        let titles = ["推荐", "游戏", "娱乐", "趣玩"]
        let titleView = PageTitleView(frame: titleFrame, titles: titles)
        titleView.delegate = self
        return titleView
    }()

    //    懒加载 PageContentView
    private lazy var pageContentView: PageContentView = {[weak self] in
        let contentFrame = CGRect(x: 0,
                                  y: mcNavBarAndStatusBarHeight + titleViewH,
                                  width: SCREEN_WIDTH,
                                  height: SCREEN_HEIGHT - (mcNavBarAndStatusBarHeight + titleViewH + mcTabBarHeight) )
       
        var childVcs = [UIViewController]()
        childVcs.append(RecommendViewController())
         
        for _ in 0 ..< 3  {
            let vc = UIViewController()
//            vc.view.backgroundColor = UIColor(red: CGFloat(arc4random_uniform(255)),
//                                              green: CGFloat(arc4random_uniform(255)),
//                                              blue: CGFloat(arc4random_uniform(255)))
            vc.view.backgroundColor = UIColor.white
            childVcs.append(vc)
        }

        let contentView = PageContentView(frame: contentFrame,
                                          childVcs: childVcs,
                                          parentViewController: self)
        contentView.delegate = self
        return contentView
    }()
}

extension HomeViewController {
    /// 设置UI
    private func setupUI() {
//        automaticallyAdjustsScrollViewInsets = false

        setupNavigationBar()
        view.addSubview(pageTitleView)
        view.addSubview(pageContentView)
    }

    /// 设置导航栏
    private func setupNavigationBar() {
        let size = CGSize(width: 40, height: 40)
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "notifi")
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "notif", hightImageName: "notifi", size: size)
    }
}

// PageTitleViewDelegate
extension HomeViewController:PageTitleViewDelegate {
    func pageTitleView(titleView: PageTitleView, selectedIndex index: Int) {
        print(index) 
        pageContentView.setCurrentIndex(currentIndex: index)
    }
 
}

// PageContentViewDelegate
extension HomeViewController:PageContentViewDelegate {
    func pageContentView(contentView: PageContentView, progress: CGFloat, sourceIndex: Int, tagetIndex: Int) {
        pageTitleView.setTitleWithProgress(progress: progress, sourceIndex: sourceIndex, targetIndex: tagetIndex)
    }
}
